﻿
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;

namespace EasyCore.DataValidation
{
    public class EasyObjectModelValidator : ObjectModelValidator
    {
        /// <summary>
        /// 是否禁用对象模型
        /// </summary>
        public bool DisableBuiltInModelValidation { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="modelMetadataProvider"></param>
        /// <param name="validatorProviders"></param>
        /// <param name="disableBuiltInModelValidation"></param>
        public EasyObjectModelValidator(IModelMetadataProvider modelMetadataProvider, IList<IModelValidatorProvider> validatorProviders, bool disableBuiltInModelValidation=false) 
            : base(modelMetadataProvider, validatorProviders)
        {
            DisableBuiltInModelValidation = disableBuiltInModelValidation;
        }

        /// <summary>
        /// 获得验证访问器
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="validatorProvider"></param>
        /// <param name="validatorCache"></param>
        /// <param name="metadataProvider"></param>
        /// <param name="validationState"></param>
        /// <returns></returns>
        public override ValidationVisitor GetValidationVisitor(ActionContext actionContext, IModelValidatorProvider validatorProvider, ValidatorCache validatorCache, IModelMetadataProvider metadataProvider, ValidationStateDictionary? validationState)
        {
            return new EasyValidationVisitor(actionContext, validatorProvider, validatorCache, metadataProvider, validationState, DisableBuiltInModelValidation);
        }
    }
}
